package com.example.allshengmedical.system.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.SearchManager.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allsheng.widget.AllShengDialog;
import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.experimentanalyze.activity.PlateFragment;
import com.example.allshengmedical.mainmenu.activity.MainMenuActivity;
import com.example.allshengmedical.system.activity.FragmentTemp1.CallBackValue;
import com.example.allshengmedical.system.presenter.TempCalibrationPresenter;
import com.example.allshengmedical.system.view.ITempCalibrationView;
import com.example.allshengmedical.util.ByAlert;
import com.itboye.allshengmedical.sci.DefaultSerialControl;
import com.itboye.allshengmedical.sci.ISerialControl;
import com.itboye.allshengmedical.sci.cmd.req.CmdType;
import com.itboye.allshengmedical.sci.cmd.req.EndVerifyTempCmd;
import com.itboye.allshengmedical.sci.packet.resp.BaseRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.GetTempRespPacket;

public class TempCalibrationActivity extends BaseActivity
		implements ITempCalibrationView, CallBackValue, OnClickListener {
	LinearLayout fragment_lin;
	TextView temp1;
	TextView temp2;
	TextView temp3;
	TempCalibrationPresenter calibrationPresenter;
	FragmentTemp1 fragmentTemp1;
	FragmentTemp2 fragmentTemp2;
	FragmentTemp3 fragmentTemp3;
	boolean isChange = true;
	ISerialControl controlInstance;

	@Override
	protected int setcontent() {
		// TODO Auto-generated method stub
		return R.layout.activity_temp_calibration;
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		controlInstance = DefaultSerialControl.getInstance();
		controlInstance.init();
		controlInstance.start();
		controlInstance.setHandler(mHandler);
		controlInstance.addCommand(CmdType.EnterVerifyTemp, "start", 0);
		initTemp1Fragment();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		calibrationPresenter = new TempCalibrationPresenter(this);
		calibrationPresenter.initAppHead();
		calibrationPresenter.changeFragment();
	}

	public Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ISerialControl.FindPacketMessage:
				// 完成主界面更新,拿到数据
				update(msg.getData());
				break;
			case ISerialControl.SCTimeoutMessage:
				String cmdType = msg.getData().getString("cmdType");
				if (cmdType.length() < 1)
					return;
				// 根据type 转换packet 为你想要的
				CmdType type = CmdType.getCmdType(cmdType.substring(1));
				// 发送指令后这里会收到数据
				Log.d("hu timeout", "type=" + cmdType + type.toDesc());
				switch (type) {
				case EnterVerifyTemp:
					final AllShengDialog allShengDialog = new AllShengDialog(TempCalibrationActivity.this, "", "超时", "ok");
					allShengDialog.show();
					allShengDialog.setClicklistener(new ClickListenerInterface() {

						@Override
						public void OK() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();
						}

						@Override
						public void Cancle() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();

						}
					});
					break;
				}
			}
		}

		private void update(Bundle bundle) {
			// TODO Auto-generated method stub
			String cmdType = bundle.getString("cmdType");
			BaseRespPacket packet = (BaseRespPacket) bundle.getSerializable("packet");
			if (cmdType.length() < 1)
				return;
			// 根据type 转换packet 为你想要的
			CmdType type = CmdType.getCmdType(cmdType.substring(1));
			// 发送指令后这里会收到数据
			Log.d("read_data", "接受到的响应数据包是" + cmdType + type.toDesc());
			switch (type) {
			case EnterVerifyTemp:
				ByAlert.alert("进入较温状态");
				break;
			}
		}
	};

	@Override
	public void initAppHead() {
		// TODO Auto-generated method stub
		setSystemTime();
		setActionBar();
	}

	private void setActionBar() {
		// TODO Auto-generated method stub
		Button leftfirstBtn;
		Button leftsecond_btn;
		TextView title;
		leftfirstBtn = BindView(R.id.leftfirst_btn);
		leftsecond_btn = BindView(R.id.leftsecond_btn);
		title = BindView(R.id.title);
		leftfirstBtn.setVisibility(View.VISIBLE);
		leftsecond_btn.setVisibility(View.VISIBLE);
		leftsecond_btn.setText(R.string.item_actionbar_home);
		leftfirstBtn.setText(R.string.item_actionbar_back);
		title.setText("温度设置");
		leftsecond_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(TempCalibrationActivity.this, MainMenuActivity.class);
				startActivity(intent);
				finish();
			}
		});
		leftfirstBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(TempCalibrationActivity.this, OpticalActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	private void setSystemTime() {
		// TODO Auto-generated method stub
		TextView date, fileName;
		date = BindView(R.id.date);
		fileName = BindView(R.id.file_name);

		// 获取系统时间
		SimpleDateFormat sdfDate = new SimpleDateFormat("MM dd,yyyy");
		date.setText(sdfDate.format(new Date()));

		// 文件路径置空
		fileName.setText("");

	}

	@Override
	public void changeFragment() {
		// TODO Auto-generated method stub

	}

	private void initTemp1Fragment() {
		// TODO Auto-generated method stub
		temp1.setBackgroundColor(getResources().getColor(R.color.blue));
		temp2.setBackgroundColor(getResources().getColor(R.color.white));
		temp3.setBackgroundColor(getResources().getColor(R.color.white));
		temp1.setTextColor(getResources().getColor(R.color.white));
		temp2.setTextColor(getResources().getColor(R.color.black));
		temp3.setTextColor(getResources().getColor(R.color.black));
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		if (fragmentTemp1 == null) {
			fragmentTemp1 = new FragmentTemp1();
			transaction.add(R.id.fragment_lin, fragmentTemp1);
		}
		hideFragment(transaction);
		transaction.show(fragmentTemp1);
		// if (fragmentTemp1 == null) {
		// fragmentTemp1 = new FragmentTemp1();
		//
		// }
		// transaction.replace(R.id.fragment_lin, fragmentTemp1);
		transaction.commit();
	}

	private void initTemp2Fragment() {
		// TODO Auto-generated method stub
		temp2.setBackgroundColor(getResources().getColor(R.color.blue));
		temp1.setBackgroundColor(getResources().getColor(R.color.white));
		temp3.setBackgroundColor(getResources().getColor(R.color.white));
		temp2.setTextColor(getResources().getColor(R.color.white));
		temp1.setTextColor(getResources().getColor(R.color.black));
		temp3.setTextColor(getResources().getColor(R.color.black));
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		if (fragmentTemp2 == null) {
			fragmentTemp2 = new FragmentTemp2();
			transaction.add(R.id.fragment_lin, fragmentTemp2);
		}
		// transaction.replace(R.id.fragment_lin, plateFragment);
		hideFragment(transaction);
		transaction.show(fragmentTemp2);
		// if (fragmentTemp2 == null) {
		// fragmentTemp2 = new FragmentTemp2();
		// }
		// transaction.replace(R.id.fragment_lin, fragmentTemp2);
		transaction.commit();
	}

	private void initTemp3Fragment() {
		// TODO Auto-generated method stub
		temp3.setBackgroundColor(getResources().getColor(R.color.blue));
		temp1.setBackgroundColor(getResources().getColor(R.color.white));
		temp2.setBackgroundColor(getResources().getColor(R.color.white));
		temp3.setTextColor(getResources().getColor(R.color.white));
		temp1.setTextColor(getResources().getColor(R.color.black));
		temp2.setTextColor(getResources().getColor(R.color.black));
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		if (fragmentTemp3 == null) {
			fragmentTemp3 = new FragmentTemp3();
			transaction.add(R.id.fragment_lin, fragmentTemp3);
		}
		// transaction.replace(R.id.fragment_lin, plateFragment);
		hideFragment(transaction);
		transaction.show(fragmentTemp3);
		// if (fragmentTemp3 == null) {
		// fragmentTemp3 = new FragmentTemp3();
		// }
		// transaction.replace(R.id.fragment_lin, fragmentTemp3);
		transaction.commit();
	}

	private void hideFragment(FragmentTransaction transaction) {
		if (fragmentTemp3 != null) {
			transaction.hide(fragmentTemp3);
		}
		if (fragmentTemp2 != null) {
			transaction.hide(fragmentTemp2);
		}
		if (fragmentTemp1 != null) {
			transaction.hide(fragmentTemp1);
		}
	}

	@Override
	public void exitFunction() {
		// TODO Auto-generated method stub

	}

	@Override
	public void SendMessageValue(String strValue) {
		// TODO Auto-generated method stub
		if ("isChange".equals(strValue)) {
			isChange = true;
			isEnalbe();
		} else {
			isChange = false;
			isEnalbe();
		}
	}

	void isEnalbe() {
		if (isChange) {
			temp1.setEnabled(true);
			temp2.setEnabled(true);
			temp3.setEnabled(true);
		} else {
			temp1.setEnabled(false);
			temp2.setEnabled(false);
			temp3.setEnabled(false);
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		controlInstance.addCommand(CmdType.EnterVerifyTemp, "end", 0);
		SystemClock.sleep(1000);
		controlInstance.pause();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.temp1:
			initTemp1Fragment();
			break;
		case R.id.temp2:
			initTemp2Fragment();
			break;
		case R.id.temp3:
			initTemp3Fragment();
			break;

		default:
			break;
		}
	}
}
